//
//  FCResetPasswordVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/2/7.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCResetPasswordVC.h"
#import "FCLoginVC.h"
@interface FCResetPasswordVC ()<UITextFieldDelegate>

/* 密码 */
@property (strong, nonatomic) UITextField *passwordField;
//确定密码
@property (strong, nonatomic) UITextField *confirmField;
/* 完成按钮 */
@property (strong, nonatomic) UIButton *completeBtn;
@end

@implementation FCResetPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [FCLocalUserHelper saveIsMainVC:@"0"];
    //监听键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    self.showBackItem = YES;
    [self setupUI];
}
-(void)setupUI{
    //背景图片
    UIImageView *bgImageView = [UIImageView new];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    bgImageView.image = [UIImage imageNamed:@"bg_login"];
    //重置密码label
    UILabel *resetPasswordLabel = [UILabel new];
    [self.view addSubview:resetPasswordLabel];
    [resetPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                make.left.mas_equalTo(32*KScreenWidthScale750);
                make.top.mas_equalTo((100)*KScreenHeightScale1334);
                
            }else{
                make.left.mas_equalTo(32*KScreenWidthScale750);
                make.top.mas_equalTo(100*KScreenHeightScale1334);
                
            }
        }else{
            make.left.mas_equalTo(32*KScreenWidthScale750);
            make.top.mas_equalTo(100*KScreenHeightScale1334);
            
        }
    }];
    resetPasswordLabel.text = Localized(@"重设密码");
    resetPasswordLabel.font = KOverstrikingfont(52);
    
    //提示语
    UILabel *hintLabel = [UILabel new];
    [self.view addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(resetPasswordLabel);
        make.top.mas_equalTo(resetPasswordLabel.mas_bottom).offset(30*KScreenHeightScale1334);
        make.right.mas_equalTo(-55*KScreenWidthScale750);
    }];
    hintLabel.font = Kfont(28);
    hintLabel.textColor = kGrayColor;
    hintLabel.text = Localized(@"请设置一个新的密码，并牢记在心。");
    hintLabel.numberOfLines = 2;
    //密码
    UILabel *passwordLabel = [UILabel new];
    [self.view addSubview:passwordLabel];
    [passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(resetPasswordLabel);
        make.top.mas_equalTo(hintLabel.mas_bottom).offset(120*KScreenHeightScale1334);
        
    }];
    passwordLabel.font = Kfont(32);
    passwordLabel.textColor = kGrayColor;
    passwordLabel.text = Localized(@"密码");
    //输入密码
    UITextField *passwordField = [UITextField new];
    self.passwordField = passwordField;
    [self.view addSubview:passwordField];
    [passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordLabel.mas_bottom).offset(30*KScreenHeightScale1334);
        make.left.mas_equalTo(resetPasswordLabel);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    passwordField.delegate = self;
    passwordField.secureTextEntry = YES;
    //分割线
    UIView *viewLine1 =[UIView new];
    [self.view addSubview:viewLine1];
    [viewLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(passwordField);
        make.top.mas_equalTo(passwordField.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    viewLine1.backgroundColor =kLineColor;
    //确定密码标题
    UILabel *confirmLabel = [UILabel new];
    [self.view addSubview:confirmLabel];
    [confirmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(resetPasswordLabel);
        make.top.mas_equalTo(viewLine1.mas_bottom).offset(80*KScreenHeightScale1334);
        
    }];
    confirmLabel.font = Kfont(32);
    confirmLabel.textColor = kGrayColor;
    confirmLabel.text = Localized(@"确认密码");
    //确定密码
    UITextField *confirmField = [UITextField new];
    self.confirmField = confirmField;
    [self.view addSubview:confirmField];
    [confirmField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(confirmLabel.mas_bottom).offset(30*KScreenHeightScale1334);
        make.left.mas_equalTo(resetPasswordLabel);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    confirmField.secureTextEntry = YES;
    confirmField.delegate = self;
    //分割线
    UIView *viewLine2 =[UIView new];
    [self.view addSubview:viewLine2];
    [viewLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(confirmField);
        make.top.mas_equalTo(confirmField.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    viewLine2.backgroundColor =kLineColor;
    
    //完成按钮
    UIButton *completeBtn = [UIButton new];
    self.completeBtn = completeBtn;
    [self.view addSubview:completeBtn];
    [completeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*KScreenWidthScale750);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
        make.bottom.mas_equalTo(-100*KScreenHeightScale1334);
        make.height.mas_equalTo(112*KScreenHeightScale1334);
    }];
    completeBtn.layer.cornerRadius = 5.0;
    completeBtn.layer.masksToBounds = YES;
    completeBtn.titleLabel.font = KOverstrikingfont(36);
    [completeBtn setTitle:Localized(@"完成") forState:(UIControlStateNormal)];
    [completeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [completeBtn setBackgroundColor:kLightblueColor];
    
    //事件
    [passwordField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [confirmField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [confirmField addTarget:self action:@selector(textFieldDidEndChange:) forControlEvents:UIControlEventEditingDidEnd];
    [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma mark-按钮事件(完成)
-(void)completeBtnClick{
    
    if (self.confirmField.text.trimWhitespace.length <= 0 || self.passwordField.text.trimWhitespace.length <= 0 ) {
        return;
        
    }
    if (self.passwordField.text.trimWhitespace.length  <= 0) {
        [self showAlert:Localized(@"新密码不能为空")];
        return;
    }
    if (self.confirmField.text.trimWhitespace.length  <= 0) {
        [self showAlert:Localized(@"确认密码不能为空")];
        return;
    }
    if (self.passwordField.text.trimWhitespace.length <6 || self.passwordField.text.trimWhitespace.length >16) {
        [self showAlert:Localized(@"密码必须为6-16位字符，区分大小写")];
        return;
    }
    if (![self.passwordField.text.trimWhitespace isEqualToString: self.confirmField.text.trimWhitespace]) {
        [self showAlert:Localized(@"密码和确认密码不一致")];
        return;
    }
    [self showChrysanthemumAlert:nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    [dict setObject:self.passwordField.text.trimWhitespace forKey:@"passWord"];
    [dict setObject:self.emailStr forKey:@"email"];
    [dict setObject:self.emailToken forKey:@"emailToken"];
    
    NSString *url = Reset_password_email_URL;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            NSDictionary *dic = responseObject;
            NSNumber *two = [NSNumber numberWithInteger:200];
            NSNumber *four = [NSNumber numberWithInteger:406];
            if (dic[@"code"] == two) {
                [self hideProgress];
//                [self showAlert:Localized(@"更新成功")];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    FCLoginVC *vc = [FCLoginVC new];
                    [self.navigationController pushViewController:vc animated:YES];
                });
               
            }else if (dic[@"code"] == four){
                [self hideProgress];
                [self showAlert:Localized(@"邮箱没有通过验证")];
            }
            
        }
        
    } faile:^(NSError *error) {
        if (error) {
             NSLog(@"%@",error);
        }
        
    }];

}
-(void)textFieldDidChange:(UITextField *)textField{
    
    
    if(self.passwordField == textField || self.confirmField == textField){
        if (textField.text.trimWhitespace.length > 16) {
            textField.text = [textField.text substringToIndex:16];
            
        }
    }
}
-(void)textFieldDidEndChange:(UITextField *)textField{
    if(self.confirmField == textField ){
    if (self.confirmField.text.trimWhitespace.length > 0 && self.passwordField.text.trimWhitespace.length > 0) {
        
        [self.completeBtn setBackgroundColor:kRedOrangeColor];
    }else{
        [self.completeBtn setBackgroundColor:kLightblueColor];
    }
        
    }
}
#pragma mark - 键盘弹出时界面上移及还原
-(void)keyBoardWillShow:(NSNotification *) notification{
    
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int keyBoardHeight = keyboardRect.size.height;
    
    //使视图上移
    CGRect viewFrame = self.view.frame;
    
    viewFrame.origin.y = -keyBoardHeight/2;//上移1/2的键盘高度
    self.view.frame = viewFrame;
    
}

- (void)keyBoardWillHide:(NSNotification *) notification {
    //当键盘消失时执行方法使视图下滑回原位
    NSTimeInterval time = [[notification.userInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    [UIView animateWithDuration:time animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = 64;
        self.view.frame = frame;
        [self.view layoutIfNeeded];
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //移除第一响应
    [self.passwordField resignFirstResponder];
    [self.confirmField resignFirstResponder];
}
@end
